/**
 * 
 */
package cc.rico.jms.utils;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.TopicConnection;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * jsm conneciton util
 * @author rico 2016年8月2日
 *
 */
public class ActiveMQConnectionUtils {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(ActiveMQConnectionFactory.class);
	
	private static ActiveMQConnectionFactory connectionFactory;
	static {
		// init connection factory
		String brokerURL = "tcp://192.168.223.137:61616";
		connectionFactory = new ActiveMQConnectionFactory(brokerURL);
	}
	
	/**
	 * get basic connection
	 * @return
	 */
	public static Connection createConnection() {
		try {
			return connectionFactory.createConnection();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * get basic connection by username and password
	 * @param username
	 * @param password
	 * @return
	 */
	public static Connection createConnection(String username, String password) {
		try {
			return connectionFactory.createConnection(username, password);
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * get topic connection
	 * @return
	 */
	public static TopicConnection createTopicConnection() {
		try {
			return connectionFactory.createTopicConnection();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * get topic connection by username and password
	 * @param username
	 * @param password
	 * @return
	 */
	public static TopicConnection createTopicConnection(String username, String password) {
		try {
			return connectionFactory.createTopicConnection(username, password);
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * get queue connection
	 * @return
	 */
	public static QueueConnection createQueueConnection() {
		try {
			return connectionFactory.createQueueConnection();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * get queue connection by username and password
	 * @param username
	 * @param password
	 * @return
	 */
	public static QueueConnection createQueueConnection(String username, String password) {
		try {
			return connectionFactory.createQueueConnection(username, password);
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * close connnection
	 * @param connection
	 */
	public static void close(Connection connection) {
		try {
			if(connection != null) {
				connection.close();
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
}
